package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AdminAddCategoryDto;
import com.feng.domain.dto.AdminCategoryDto;
import com.feng.domain.entity.Article;
import com.feng.domain.vo.*;
import com.feng.mapper.CategoryMapper;
import com.feng.domain.entity.Category;
import com.feng.service.ArticleService;
import com.feng.service.CategoryService;
import com.feng.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-04-13 20:08:02
 */
@Service()
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategory() {
        //查询文章 状态为发布的
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleService.list(queryWrapper);

        //转换为id集合
        Set<Long> articleIds = articles.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //查询分类表
        List<Category> categories = listByIds(articleIds);

        //筛选重复的 状态为发布的
        categories= categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());

        //vo转换
        List<CategoryVo> categoryVos = BeanCopyUtil.articleListVo(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> categories = list(wrapper);
        List<CategoryListVo> categoryListVos = BeanCopyUtil.articleListVo(categories, CategoryListVo.class);
        return ResponseResult.okResult(categoryListVos);
    }

    @Override
    public ResponseResult getList(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), Category::getName, name);
        wrapper.eq(StringUtils.hasText(status), Category::getStatus, status);
        Page<Category> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, wrapper);
        List<AdminCategoryVo> adminCategoryVo = BeanCopyUtil.articleListVo(page.getRecords(), AdminCategoryVo.class);
        return ResponseResult.okResult(new ArticleListPageVo(adminCategoryVo, page.getTotal()));
    }

    @Override
    public ResponseResult addCategory(AdminAddCategoryDto categoryDto) {
        Category category = BeanCopyUtil.articleVo(categoryDto, Category.class);
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getUpdateList(Long id) {
        Category category = getById(id);
        AdminUpdateCategoryVo categoryVo = BeanCopyUtil.articleVo(category, AdminUpdateCategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @Override
    public ResponseResult updateCategory(AdminCategoryDto categoryDto) {
        Category category = BeanCopyUtil.articleVo(categoryDto, Category.class);
        updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delCategory(List<Long> ids) {
        removeBatchByIds(ids);
        return ResponseResult.okResult();
    }
}

